home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / encrypt2 / encrypt.frm (.txt) next >
Encoding:
Visual Basic Form  |  1997-08-17  |  3.9 KB  |  141 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Encryption Example"
  4.    ClientHeight    =   3750
  5.    ClientLeft      =   1665
  6.    ClientTop       =   1740
  7.    ClientWidth     =   6690
  8.    Height          =   4155
  9.    Left            =   1605
  10.    LinkTopic       =   "Form1"
  11.    LockControls    =   -1  'True
  12.    ScaleHeight     =   3750
  13.    ScaleWidth      =   6690
  14.    Top             =   1395
  15.    Width           =   6810
  16.    Begin VB.CommandButton Command2 
  17.       Caption         =   "Decrypt"
  18.       Height          =   375
  19.       Left            =   4800
  20.       TabIndex        =   3
  21.       Top             =   3120
  22.       Width           =   1455
  23.    End
  24.    Begin VB.CommandButton Command1 
  25.       Caption         =   "Encrypt"
  26.       Height          =   375
  27.       Left            =   3000
  28.       TabIndex        =   2
  29.       Top             =   3120
  30.       Width           =   1575
  31.    End
  32.    Begin VB.TextBox Text2 
  33.       Height          =   375
  34.       Left            =   480
  35.       TabIndex        =   1
  36.       Top             =   3120
  37.       Width           =   2295
  38.    End
  39.    Begin VB.TextBox Text1 
  40.       Height          =   2535
  41.       Left            =   480
  42.       MultiLine       =   -1  'True
  43.       ScrollBars      =   3  'Both
  44.       TabIndex        =   0
  45.       Top             =   360
  46.       Width           =   5775
  47.    End
  48. Attribute VB_Name = "Form1"
  49. Attribute VB_Creatable = False
  50. Attribute VB_Exposed = False
  51. Option Explicit
  52.    Dim Key(1 To 20) As Integer
  53.    Private Type cryptrecord
  54.       code As Integer
  55.    End Type
  56. Private Sub Command1_Click()
  57.    Dim crypt As cryptrecord
  58.    Dim n As Long
  59.    Dim i As Integer
  60.    If Len(Text2.Text) < 3 Then
  61.       MsgBox "Password must be 3 to 20 characters long.", 0, "Password Needed"
  62.    Else
  63.       
  64.       'If password is longer than 20, only use the first 20 characters...
  65.       
  66.       If Len(Text2.Text) > 20 Then
  67.          Text2.Text = Left(Text2.Text, 20)
  68.       End If
  69.       
  70.       'We'll use the length of the password to repeat it...
  71.       
  72.       For i = 1 To Len(Text2.Text)
  73.          Key(i) = Asc(Mid(Text2.Text, i, 1)) + i
  74.       Next
  75.       
  76.       'Set i = 1 to be able to increment through Key(i)...
  77.       
  78.       i = 1
  79.       
  80.       'Open the file...
  81.       
  82.       Open "c:\coded.dat" For Random As #1 Len = Len(crypt)
  83.       For n = 1 To Len(Text1.Text)
  84.          
  85.          'Add the ASCII value of Key(i) to the ASCII value
  86.          'of the next character in Text1.Text...
  87.          
  88.          crypt.code = Asc(Mid(Text1.Text, n, 1)) + Key(i)
  89.          
  90.          'Increment i...
  91.          
  92.          i = i + 1
  93.          
  94.          'If i is larger than the lenght of the password, reset it...
  95.          
  96.          If i > Len(Text2.Text) Then
  97.             i = 1
  98.          End If
  99.          
  100.          Put #1, n, crypt.code
  101.       Next
  102.       Close #1
  103.       Text1.Text = "Done!"
  104.       Text2.Text = ""
  105.    End If
  106. End Sub
  107. Private Sub Command2_Click()
  108.    Dim crypt As cryptrecord
  109.    Dim n, filelength As Long
  110.    Dim i As Integer
  111.    Dim temp As String * 1
  112.    Text1.Text = ""
  113.    If Len(Text2.Text) < 3 Then
  114.       MsgBox "Password must be 3 to 20 characters long.", 0, "Password Needed"
  115.    Else
  116.       If Len(Text2.Text) > 20 Then
  117.          Text2.Text = Left(Text2.Text, 20)
  118.       End If
  119.       For i = 1 To Len(Text2.Text)
  120.          Key(i) = Asc(Mid(Text2.Text, i, 1)) + i
  121.       Next
  122.       i = 1
  123.       Open "c:\coded.dat" For Random As #1 Len = Len(crypt)
  124.       
  125.       'Divide the file length (bytes) by 2. We used integers
  126.       'which take 2 bytes each...
  127.       
  128.       filelength = LOF(1) / 2
  129.       For n = 1 To filelength
  130.          Get #1, n, crypt.code
  131.          temp = Chr(Abs(crypt.code - Key(i)))
  132.          i = i + 1
  133.          If i > Len(Text2.Text) Then
  134.             i = 1
  135.          End If
  136.          Text1.Text = Text1.Text & temp
  137.       Next
  138.       Close #1
  139.    End If
  140. End Sub
  141.